﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Twitterizer;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace Twillo.Core.Controls
{
    public partial class UserTweetBanner : UserControlBase
    {
        [Category("Twitter")]
        public TwitterStatus TwitterStatus { get { return _status; } set { _status = value; RefreshControl(); } }
        [Category("Events")]
        public event LinkClickedHandler LinkClicked;
        public delegate void LinkClickedHandler(object sender, TwitterStatusLabelClickEventArgs e);

        private TwitterStatus _status = null;

        public UserTweetBanner()
        {
            InitializeComponent();
        }

        protected void lblDescription_LinkClicked(object sender, TwitterStatusLabelClickEventArgs e)
        {
            if (LinkClicked != null) { LinkClicked(this, e); }
        }

        private void RefreshControl()
        {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("<a href=\"(?<url>.*)\" rel=\"nofollow\">(?<title>.*)</a>");
            ToolTip tip = new ToolTip();
            lblDescription.TwitterStatus = null;
            lblDescription.LinkClicked += new TwitterStatusLabel.LinkClickedHandler(lblDescription_LinkClicked);
            lblScreenName.Text = "";
            tip.SetToolTip(this, "");
            if (this.DesignMode) { return; }
            if (_status == null) { return; }
            lblDescription.BackColor = this.BackColor;
            lblDescription.TwitterStatus = _status;
            lblScreenName.Text = _status.User.ScreenName;
            tip.SetToolTip(this, _status.Text);
            pbIcon.Image = LoadImage(_status.User.ProfileImageLocation);
            Match m = regex.Match(_status.Source);
            if (m.Success)
            {
                string url = m.Groups["url"].Value;
                LinkLabel lbl = new LinkLabel();
                lbl.ForeColor = System.Drawing.Color.Gray;
                lbl.AutoSize = true;
                lbl.Location = new Point(57, 67);
                tip.SetToolTip(lbl, url);
                lbl.Text = _status.CreatedDate.ToLongDateString() + " " + _status.CreatedDate.ToShortTimeString() + " via " + m.Groups["title"].Value;
                this.Controls.Add(lbl);
            }
            else
            {
                Label lbl = new Label();
                lbl.ForeColor = System.Drawing.Color.Gray;
                lbl.Location = new Point(57, 67);
                lbl.AutoSize = true;
                lbl.Text = _status.CreatedDate.ToLongDateString() + " " + _status.CreatedDate.ToShortTimeString() + " via " + _status.Source;
                this.Controls.Add(lbl);
            }

        }

        private void cRetweet_Click(object sender, EventArgs e)
        {
            Twitter.ReTweet(_status);
            RefreshControl();
        }

        private void cDirectMsg_Click(object sender, EventArgs e)
        {
            string text = string.Empty;

            if (!string.IsNullOrEmpty(text))
            {
                Twitter.SendDirectMessage(_status.User.ScreenName, text);
            }
        }

        private void lblScreenName_Click(object sender, EventArgs e)
        {
            if (this.LinkClicked != null)
            {
                LinkClicked(lblScreenName, new TwitterStatusLabelClickEventArgs(_status.User.ScreenName, TwitterStatusLabelLinkType.User));
            }
        }

    }
}
